{include file="pageheader_erp.htm"}

<link href="styles/erp_payment.css" rel="stylesheet" type="text/css" />

{insert_scripts files="../js/jquery.1.3.2.js"}
{insert_scripts files="../js/jquery.blockUI.2.23.js"}

{include file="erp_payment_search.htm"}

<style type="text/css">
	.current_page_style
	{
		color:red;
		font-weight:bold;
	}
	
</style>

<script language="JavaScript">

	function view_approve_remark(payment_id)
	{
		$.blockUI({   
		  message: $('#approve_remark_'+payment_id), css: { 
		  top:  ($(window).height()-300) /2 + 'px', 
		  left: ($(window).width()-500) /2 + 'px'
		  }
		});	

		$('#img_close_approve_remark_'+payment_id).click(function(){
			$.unblockUI();
			$('#img_close_approve_remark_'+payment_id).unbind('click');
		});

		$('#img_submit_approve_'+payment_id).click(function(){
			$.unblockUI();
			$('#img_submit_approve_'+payment_id).unbind('click');
		});				
	}
	
</script>

{foreach from=$payment_info item=payment}
{if $payment.payment_status eq 2 or $payment.payment_status eq 3 or $payment.payment_status eq 4}

<div class="approve_remark" id="approve_remark_{$payment.payment_id}">
<div class="approve_remark_head">
	<table cellpadding="0" cellspacing="0" bpayment='0'>
		<tr>
			<td>
				<img src="./images/approve_remark.gif" />
			</td>
			<td align="right">
				<span><img style="cursor:pointer;" id="img_close_approve_remark_{$payment.payment_id}" src="./images/close_erp.gif" /></span>
			</td>
		</tr>
	</table>
</div>

<div class="approve_remark_box">
<table  cellpadding="5" cellspacing="0" bpayment='0'>
	 <tr>
    	<td width="60">{$lang.erp_approve_remark}</td>	
		<td>
			<textarea rows="8" cols="55" disabled="true" id="textarea_approve_remark_{$payment.payment_id}">{$payment.approve_remark}</textarea>
		</td>
	</tr>	
</table>
</div>
<div class="div_submit_approve">
	<img style="cursor:pointer;" id="img_submit_approve_{$payment.payment_id}" src="./images/submit.gif" />
</div>	
</div>

{/if}
{/foreach}

{foreach from=$payment_info item=payment}

<div class="payment_info" id="payment_info_{$payment.payment_id}">
<div class="payment_info_head">
	<table cellpadding="0" cellspacing="0" bpayment='0'>
		<tr>
			<td>
				<img src="./images/payment_info.gif" />
			</td>
			<td align="right">
				<span><img style="cursor:pointer;" id="img_close_payment_info_{$payment.payment_id}" src="./images/close_erp.gif" /></span>
			</td>
		</tr>
	</table>
</div>

<div class="payment_info_box">
<table cellpadding="3" cellspacing="0">
	 <tr>
    	<td width="130">{$lang.erp_payment_sn}：</td>	
		<td height="21">
			<span>{$payment.payment_sn}</span>
		</td>
	</tr>
	<tr>
    	<td>{$lang.erp_payment_date}：</td>	
		<td height="21">
			<span>{$payment.create_date}</span>
		</td>
	</tr>
	<tr>
    	<td>{$lang.erp_payment_account}：</td>	
		<td height="21">
			<span id="span_bank_account_{$payment.payment_id}">{$payment.account_info.account_name}</span>
			{if $bank_account}
			<select name="select_bank_account_{$payment.payment_id}" id="select_bank_account_{$payment.payment_id}" style="display:none;">
				{foreach from=$bank_account item=bank_account}
					<option {if $payment.account_info.account_id eq $bank_account.account_id}selected="selected"{/if} value="{$bank_account.account_id}">{$bank_account.account_name}</option>
				{/foreach}
			</select>
			{/if}			
		</td>
	</tr>	
	<tr>
    	<td>{$lang.erp_payment_pay_to}：</td>	
		<td height="21">
			<span id="span_supplier_name_{$payment.payment_id}">{$payment.supplier_info.0.name}</span>
		</td>
	</tr>	
	<tr>
    	<td>{$lang.erp_payment_order_id}：</td>	
		<td height="21">
			<span  id="span_order_sn_{$payment.payment_id}">{$payment.order_info.order_sn}</span>
			{if $payable_orders}
			<select name="select_order_{$payment.payment_id}" id="select_order_{$payment.payment_id}" style="display:none;">
				{foreach from=$payable_orders item=payable_order}
					<option {if $payment.order_info.order_id eq $payable_order.order_id}selected="selected"{/if} value="{$payable_order.order_id}">{$payable_order.order_info.order_sn}</option>
				{/foreach}
			</select>
			{/if}
		</td>
	</tr>
	<tr>
    	<td>{$lang.erp_payment_order_date}：</td>	
		<td height="21">
			<span id="span_order_date_{$payment.payment_id}">{$payment.order_info.create_date}</span>
		</td>
	</tr>	
	<tr>
    	<td>{$lang.erp_payment_order_amount}：</td>	
		<td height="21">
			<span id="span_bill_amount_{$payment.payment_id}">{$payment.bill_amount}</span>
		</td>
	</tr>
	<tr>
    	<td>{$lang.erp_payment_order_total_payable}：</td>	
		<td height="21">
			<span id="span_total_payable_{$payment.payment_id}">{$payment.bill_total_payable}</span>
		</td>
	</tr>
	<tr>
    	<td>{$lang.erp_payment_order_paid_payable}：</td>	
		<td height="21">
			<span id="span_paid_payable_{$payment.payment_id}">{$payment.bill_paid_payable}</span>
		</td>
	</tr>
	<tr>
    	<td>{$lang.erp_payment_order_payable_balance}：</td>	
		<td height="21">
			<span id="span_payable_balance_{$payment.payment_id}" >{$payment.bill_payable_balance}</span>
		</td>
	</tr>
	<tr>
    	<td>{$lang.erp_payment_pay_amount}：</td>	
		<td height="23">
			<span  id="span_pay_amount_{$payment.payment_id}">{$payment.pay_amount}</span>
			<input id="input_pay_amount_{$payment.payment_id}" type="text" size="10" value="{$payment.pay_amount}" style="display:none;" />
		</td>
	</tr>
	<tr>
    	<td>{$lang.erp_payment_remark}：</td>	
		<td>
			<textarea rows="4" cols="45" id="textarea_payment_remark_{$payment.payment_id}" disabled="true">{$payment.payment_remark}</textarea>
		</td>
	</tr>							
</table>
</div>
<div class="div_submit_payment_info">
	<img style="cursor:pointer;" id="img_submit_payment_info_{$payment.payment_id}" src="./images/submit.gif" />
</div>	
</div>

{/foreach}

<!-- start  payment list -->
 <div class="list-div" id="listDiv">
<table  cellpadding="3" cellspacing="1">
  <tr>
    <th width="5%">{$lang.erp_payment_id}</th>
    <th width="8%">{$lang.erp_payment_sn}</th>
 	<th>{$lang.erp_payment_remark}</th>
	<th width="10%">{$lang.erp_payment_pay_to}</th>
	<th width="10%">{$lang.erp_payment_date}</th>
	<th  width="10%">{$lang.erp_payment_status}</th>
	<th>{$lang.erp_operation}</th>
  <tr>
  {foreach from=$payment_info item=payment}
  <tr id="tr_{$payment.payment_id}">
    <td align="center">{$payment.payment_id}</td>
	<td align="center">{$payment.payment_sn}</td>	
	<td><span id="span_payment_remark_{$payment.payment_id}">{$payment.payment_remark}</span></td>	
	<td >{$payment.supplier_info.0.name}</td>
	<td  align="center">{$payment.create_date}</td>
	<td align="center">
		{if $payment.payment_status eq 1}{$lang.erp_payment_status_inputing}{/if}
		{if $payment.payment_status eq 2}{$lang.erp_payment_status_approving}{/if}
		{if $payment.payment_status eq 3}<span style="color:blue;">{$lang.erp_payment_status_approve_pass}</span>{/if}
		{if $payment.payment_status eq 4}<span style="color:red;">{$lang.erp_payment_status_approve_reject}</span>{/if}
	</td>
		
	<td align="center"  width="20%">
		{if $payment.available_act.view eq 1}<a href="javascript: view_payment({$payment.payment_id})">{$lang.erp_operation_view}</a>{/if}
		{if $payment.available_act.edit eq 1}<a href="javascript: edit_payment({$payment.payment_id})">{$lang.erp_operation_edit}</a>{/if}
		{if $payment.available_act.delete eq 1}<a href="javascript:delete_payment({$payment.payment_id})">{$lang.erp_operation_delete}</a>{/if}
		
		{if $payment.available_act.post_to_approve eq 1}<a href="javascript:post_to_approve({$payment.payment_id})">{$lang.erp_operation_post_to_approve}</a>{/if}
		{if $payment.available_act.approve eq 1}
			<a href="javascript: approve_payment({$payment.payment_id},'pass')">{$lang.erp_operation_approve_pass}</a>
			<a href="javascript: approve_payment({$payment.payment_id},'reject')">{$lang.erp_operation_approve_reject}</a>
			<a href="javascript: withdrawal_to_edit({$payment.payment_id})">{$lang.erp_operation_withdrawal}</a>
		{/if}	
		
		{if $payment.payment_status eq 3 or $payment.payment_status eq 4}
			<a href="javascript: view_payment({$payment.payment_id})">{$lang.erp_operation_view}</a>
			<a href="javascript:view_approve_remark({$payment.payment_id})">{$lang.erp_payment_operation_view_approve_remark}</a>
		{/if}
		
		{if $payment.payment_status eq 3}
			<!--<a target="_blank" href="erp_finance_manage.php?act=print_payment&payment_id={$payment.payment_id}&page=1">{$lang.erp_operation_print}</a>-->
		{/if}
    </td>				
  </tr>
  {foreachelse}
  <tr><td class="no-records" colspan="10">{$lang.no_records}</td></tr>
  {/foreach}
   <tr><td colspan="10" align="right">{$page}</td></tr> 
</table>
</div>
<!-- end payment list -->

{literal}
<script language="JavaScript">

		function withdrawal_to_edit(payment_id)
		{
			function withdrawalToEditResponse(res)
			{
				if(res.error!=0)
				{
					alert(res.message);
				}
				else{
					window.document.location="./erp_finance_manage.php?act=payment_list";
				}
			}
			if(confirm('{$lang.erp_payable_sure_to_withdrawal_to_edit}'))
			{	
				Ajax.call('erp_payable.php?act=withdrawal_to_edit', 'payment_id=' + payment_id, withdrawalToEditResponse, 'POST', 'JSON');
			}			
		}

   		function edit_payment(payment_id)
		{
			Ajax.call('erp_payment.php?act=edit_payment', 'payment_id=' + payment_id, editPaymentResponse, 'POST', 'JSON');
			
			function editPaymentResponse(res)
			{
				if(res.error!=0)
				{
					alert(res.message);
					$.unblockUI();
					unbind_func();	
				}
				else{
					bind_func();
					$.blockUI({   
		 			message: $('#payment_info_'+payment_id), css: { 
		  			top:  ($(window).height()-500) /2 + 'px', 
		  			left: ($(window).width()-500) /2 + 'px'
		  			}
					});						
				}
			}
		}

   		function view_payment(payment_id)
		{
				$.blockUI({   
		 		message: $('#payment_info_'+payment_id), css: { 
		  		top:  ($(window).height()-500) /2 + 'px', 
		  		left: ($(window).width()-500) /2 + 'px'
		  		}
				});						
		
		}
		
		function unbind_func()
		{
			$("span[id^='span_order_sn_']").css('cursor','text');
			$("span[id^='span_order_sn_']").css('background-color','#FFF');		
			$("span[id^='span_order_sn_']").unbind('mouseover');
			$("span[id^='span_order_sn_']").unbind('mouseout');
			$("span[id^='span_order_sn_']").unbind('click');
			
			$("select[id^='select_order_']").unbind('blur');
			$("select[id^='select_order_']").unbind('change');
			
			$("span[id^='span_pay_amount_']").css('cursor','text');
			$("span[id^='span_pay_amount_']").css('background-color','#FFF');		
			$("span[id^='span_pay_amount_']").unbind('mouseover');
			$("span[id^='span_pay_amount_']").unbind('mouseout');
			$("span[id^='span_pay_amount_']").unbind('click');	
			
			$("input[id^='input_pay_amount_']").unbind('blur');
			$("input[id^='input_pay_amount_']").unbind('change');
			
			$("span[id^='span_bank_account_']").css('cursor','text');
			$("span[id^='span_bank_account_']").css('background-color','#FFF');	
			$("span[id^='span_bank_account_']").unbind('mouseover');
			$("span[id^='span_bank_account_']").unbind('mouseout');
			$("span[id^='span_bank_account_']").unbind('click');
			
			$("select[id^='select_bank_account_']").unbind('blur');
			$("select[id^='select_bank_account_']").unbind('change');
			
			$("textarea[id^='textarea_payment_remark_']").attr('disabled',true);
		}
		
		function bind_func()
		{
			$("textarea[id^='textarea_payment_remark_']").attr('disabled',false);
			
			$("span[id^='span_order_sn_']").mouseover(function(){
				$(this).css('color','red');
			});
			$("span[id^='span_order_sn_']").mouseout(function(){
				$(this).css('color','black');
			});			
			$("span[id^='span_order_sn_']").css('cursor','pointer');
			$("span[id^='span_order_sn_']").css('background-color','#DDEEF2');	
			
			$("span[id^='span_order_sn_']").click(function(){
				var this_id=$(this).attr('id');
				var payment_id=this_id.substr('span_order_sn_'.length,this_id.length-'span_order_sn_'.length);
				$("#select_order_"+payment_id).show().focus();
				$(this).hide();
			});					
			$("select[id^='select_order_']").blur(function(){
				var this_id=$(this).attr('id');
				var payment_id=this_id.substr('select_order_'.length,this_id.length-'select_order_'.length);
				$("#span_order_sn_"+payment_id).show();
				$(this).hide();
			});
			
			//更改付款单关联订单
			$("select[id^='select_order_']").change(function(){
				var this_id=$(this).attr('id');
				var payment_id=this_id.substr('select_order_'.length,this_id.length-'select_order_'.length);				
				var order_id=$(this).attr('value');
				
				var pay_amount=$("#input_pay_amount_"+payment_id).attr('value');
				
				if(confirm('{$lang.erp_payment_sure_to_change_order}'))
				{
					Ajax.call('erp_payment.php?act=change_order', 'payment_id='+payment_id+'&order_id='+order_id+'&pay_amount='+pay_amount, changeOrderResponse, 'POST', 'JSON');
				}
				else{
					$("#span_order_sn_"+payment_id).show();
					$("#select_order_"+payment_id).hide();	
				}
				
				function changeOrderResponse(res)
				{
					if(res.error!=0)
					{
						alert(res.message);
					}
					else{
						$("#span_supplier_name_"+payment_id).text(res.pay_to);
						$("#span_order_date_"+payment_id).text(res.bill_date);
						$("#span_bill_amount_"+payment_id).text(res.bill_amount);
						$("#span_total_payable_"+payment_id).text(res.bill_total_payable);
						$("#span_paid_payable_"+payment_id).text(res.bill_paid_payable);
						$("#span_payable_balance_"+payment_id).text(res.bill_payable_balance);
						$("#span_pay_amount_"+payment_id).text(res.pay_amount);
						
						$("#span_order_sn_"+payment_id).text(res.bill_no);;
					}
				}	
			});
			
			//更改付款金额
			$("span[id^='span_pay_amount_']").mouseover(function(){
				$(this).css('color','red');
			});
			$("span[id^='span_pay_amount_']").mouseout(function(){
				$(this).css('color','black');
			});			
			$("span[id^='span_pay_amount_']").css('cursor','pointer');				
			$("span[id^='span_pay_amount_']").css('background-color','#DDEEF2');	
			
			$("span[id^='span_pay_amount_']").click(function(){
				var this_id=$(this).attr('id');
				var payment_id=this_id.substr('span_pay_amount_'.length,this_id.length-'span_pay_amount_'.length);
				$("#input_pay_amount_"+payment_id).show().focus();
				$(this).hide();
			});					

			$("input[id^='input_pay_amount_']").blur(function(){
				var this_id=$(this).attr('id');
				var payment_id=this_id.substr('input_pay_amount_'.length,this_id.length-'input_pay_amount_'.length);
				$("#span_pay_amount_"+payment_id).show();
				$(this).hide();
			});			
			
			$("input[id^='input_pay_amount_']").change(function(){
				var this_id=$(this).attr('id');
				var payment_id=this_id.substr('input_pay_amount_'.length,this_id.length-'input_pay_amount_'.length);
				var pay_amount=$(this).attr('value');
				var payable=$("#span_payable_balance_"+payment_id).text();
				
				if(isNaN(pay_amount))
				{
					alert('{$lang.erp_not_a_number}');
				}
				else if(Number(pay_amount)==0)
				{
					$("#input_pay_amount_"+payment_id).attr('value',$("#span_pay_amount_"+payment_id).text());
					$("#input_pay_amount_"+payment_id).hide();
					$("#span_pay_amount_"+payment_id).show();					
				}
				else if(Number(pay_amount)>Number(payable))
				{
					alert('{$lang.erp_payment_greater_than_payable}');
					$("#input_pay_amount_"+payment_id).attr('value',$("#span_pay_amount_"+payment_id).text());
					$("#input_pay_amount_"+payment_id).hide();
					$("#span_pay_amount_"+payment_id).show();
				}
				else{
					
					function changePayAmountResponse(res)
					{
						if(res.error!=0)
						{
							alert(res.message);
						}
						else{
							$("#span_pay_amount_"+payment_id).text(pay_amount);
							$("#input_pay_amount_"+payment_id).hide();
							$("#span_pay_amount_"+payment_id).show();				
						}						
					}
					
					if(confirm('{$lang.erp_payment_sure_to_change_pay_amount}'))
					{
						Ajax.call('erp_payment.php?act=change_pay_amount', 'payment_id='+payment_id+'&pay_amount='+pay_amount, changePayAmountResponse, 'POST', 'JSON');
					}				
					else{
						$("#input_pay_amount_"+payment_id).attr('value',$("#span_pay_amount_"+payment_id).text());
						$("#input_pay_amount_"+payment_id).hide();
						$("#span_pay_amount_"+payment_id).show();					
					}
				}
				
			});		

			//更改付款帐号
			$("span[id^='span_bank_account_']").mouseover(function(){
				$(this).css('color','red');
			});
			$("span[id^='span_bank_account_']").mouseout(function(){
				$(this).css('color','black');
			});			
			$("span[id^='span_bank_account_']").css('cursor','pointer');				
			$("span[id^='span_bank_account_']").css('background-color','#DDEEF2');	
			
			$("span[id^='span_bank_account_']").click(function(){
				var this_id=$(this).attr('id');
				var payment_id=this_id.substr('span_bank_account_'.length,this_id.length-'span_bank_account_'.length);
				$("#select_bank_account_"+payment_id).show().focus();
				$(this).hide();
			});		
			$("select[id^='select_bank_account_']").blur(function(){
				var this_id=$(this).attr('id');
				var payment_id=this_id.substr('select_bank_account_'.length,this_id.length-'select_bank_account_'.length);
				$("#span_bank_account_"+payment_id).show();
				$(this).hide();
			});			
			$("select[id^='select_bank_account_']").change(function(){
				var this_id=$(this).attr('id');
				var payment_id=this_id.substr('select_bank_account_'.length,this_id.length-'select_bank_account_'.length);				
				var bank_account_id=$(this).attr('value');
				
				if(confirm('{$lang.erp_payment_sure_to_change_bank_account}'))
				{
					Ajax.call('erp_payment.php?act=change_bank_account', 'payment_id='+payment_id+'&bank_account_id='+bank_account_id, changeBankAccountResponse, 'POST', 'JSON');
				}				
				else{
					$("#span_bank_account_"+payment_id).show();
					$("#select_bank_account_"+payment_id).hide();
				}
				
				function changeBankAccountResponse(res)
				{
					if(res.error!=0)
					{
						alert(res.message);
					}
					else{
						//alert(res.account_name);
						$("#span_bank_account_"+payment_id).text(res.account_name);
						$("#select_bank_account_"+payment_id).hide();
						$("#span_bank_account_"+payment_id).show();
					}					
				}
			});
			
		}


		$(document).ready(function(){
		
			$("img[id^='img_close_payment_info_']").click(function(){
				$.unblockUI();
				unbind_func();
			});

			$("img[id^='img_submit_payment_info_']").click(function(){
				var this_id=$(this).attr('id');
				var payment_id=this_id.substr('img_submit_payment_info_'.length,this_id.length-'img_submit_payment_info_'.length);
				var payment_remark=$("#textarea_payment_remark_"+payment_id).attr('value');
				
				if($("#textarea_payment_remark_"+payment_id).attr('disabled')==false)
				{
					Ajax.call('erp_payment.php?act=change_payment_remark', 'payment_id='+payment_id+'&payment_remark='+payment_remark, changePaymentRemarkResponse, 'POST', 'JSON');
				}
				else{
					$.unblockUI();
					unbind_func();					
				}
				
				function changePaymentRemarkResponse(res)
				{
					if(res.error!=0)
					{
						alert(res.message);
					}
					else{
						$("#span_payment_remark_"+payment_id).text(payment_remark);
						$.unblockUI();
						unbind_func();
					}						
				}
			});		
		
		});

	//审核订单
	function approve_payment(payment_id,act)
	{
		function approvePaymentResponse(res)
		{
			if(res.error!=0)
			{
				alert(res.message);
			}
			else{
				alert(res.message);
				window.document.location="./erp_finance_manage.php?act=payment_list";
			}
		}
		
		$("#img_close_approve_remark_"+payment_id).click(function(){
			$.unblockUI();
			$(this).unbind('click');
		});
		
		$("#textarea_approve_remark_"+payment_id).attr('disabled',false);
		
		if(act=='pass')
		{
			$("#img_submit_approve_"+payment_id).bind('click',function(){
				var approve_remark=$("#textarea_approve_remark_"+payment_id).attr('value');
				if(confirm('{$lang.erp_payment_sure_to_approve}'))
				{
					Ajax.call('erp_payment.php?act=approve_pass', 'payment_id='+payment_id+'&approve_remark='+approve_remark, approvePaymentResponse, 'POST', 'JSON');
				}
				else{
					$(this).unbind('click');
					$("#img_close_approve_remark_"+payment_id).click();			
				}					
			});

			$.blockUI({
		   	message: $('#approve_remark_'+payment_id), css: { 
		    top:  ($(window).height()-200) /2 + 'px', 
		    left: ($(window).width()-500) /2 + 'px'
		   	}
			});	
		}
		else if(act=='reject')
		{
			$("#img_submit_approve_"+payment_id).bind('click',function(){
				var approve_remark=$("#textarea_approve_remark"+payment_id).attr('value');
				if(confirm('{$lang.erp_payment_sure_to_approve}'))
				{
					Ajax.call('erp_payment.php?act=approve_reject', 'payment_id='+payment_id+'&approve_remark='+approve_remark, approvePaymentResponse, 'POST', 'JSON');
				}
				else{
					$(this).unbind('click');
					$("#img_close_approve_remark_"+payment_id).click();				
				}					
				});
				$.blockUI({
		   		message: $('#approve_remark_'+payment_id), css: { 
		   	 	top:  ($(window).height()-200) /2 + 'px', 
		   	 	left: ($(window).width()-500) /2 + 'px'
		   }			
			});
		}
	}	
	
	
		function post_to_approve(payment_id)
		{
			function postToApproveResponse(res)
			{
				if(res.error!=0)
				{
					alert(res.message);
				}
				else{
					alert(res.message);
					window.document.location="./erp_finance_manage.php?act=payment_list";
				}
			}
			if(confirm('{$lang.erp_payment_sure_to_post_payment}'))
			{	
				Ajax.call('erp_payment.php?act=post_to_approve', 'payment_id=' + payment_id, postToApproveResponse, 'POST', 'JSON');
			}
		}

		function delete_payment(payment_id)
		{
				function deletePaymentResponse(res)
				{
					if(res.error==4)
					{
						alert(res.message);
						window.document.location="./erp_finance_manage.php?act=payment_list";
					}
					else if(res.error==0)
					{
						alert(res.message);
						window.document.location="./erp_finance_manage.php?act=payment_list";
					}
					else{
						alert(res.message);
					}
				}
				
				if(confirm('{$lang.erp_payment_sure_to_delete_payment}'))
				{
					Ajax.call('erp_payment.php?act=delete_payment', 'payment_id=' + payment_id, deletePaymentResponse, 'POST', 'JSON');
				}
			}
	
 
</script>
{/literal}
{include file="pagefooter.htm"}

